package com.three.lab.page.address;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.three.lab.R;
import com.three.lab.adapter.AddressListAdapter;
import com.three.lab.base.AppConstant;
import com.three.lab.base.AppManager;
import com.three.lab.base.BaseActivity;
import com.three.lab.bean.AddressBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 地址管理
 * created by lqd on 2024-06-16.
 */
public class AddressListActivity extends BaseActivity {

    private TextView tvTitle,tvLoading;
    private View mLoadingView;
    private ImageView ivState;
    private ProgressBar progressBar;
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private AddressListAdapter adapter;

    @Override
    protected void initParms(Bundle bundle) {}

    @Override
    protected int bindLayout() {
        return R.layout.activity_address_list;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
        tvTitle = $(R.id.tv_title);
        tvTitle.setText(R.string.address_manager);
        $(R.id.iv_right).setVisibility(View.VISIBLE);

        mLoadingView = LayoutInflater.from(this).inflate(R.layout.loading_view,null);
        ivState = mLoadingView.findViewById(R.id.iv_state);
        tvLoading = mLoadingView.findViewById(R.id.tv_loading);
        progressBar = mLoadingView.findViewById(R.id.progressBar);
        refreshLayout = $(R.id.refresh_layout);
        recyclerView = $(R.id.refresh_recycleview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new AddressListAdapter();
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void setListener() {
        $(R.id.iv_left).setOnClickListener(this);
        $(R.id.iv_right).setOnClickListener(this);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onRefreshing();
            }
        });
        tvLoading.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefreshing();
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                AddressBean bean = (AddressBean) baseQuickAdapter.getItem(position);
                int id = view.getId();
                switch (id){
                    case R.id.iv_edit:
                        onEdit(bean,"edit");
                        break;
                }
            }
        });
    }

    private void onEdit(AddressBean bean, String edit) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(AppConstant.EXTRAS_BEAN,bean);
        startActivity(AddressEditActivity.class,bundle);
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()){
            case R.id.iv_left:
                finish();
                break;
            case R.id.iv_right:
                onEdit(null,"new");
                break;
        }
    }

    @Override
    protected void initData(Context mContext) {
        onRefreshing();
    }

    private void onRefreshing() {
        if (refreshLayout != null && refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(true);
        }
        progressBar.setVisibility(View.VISIBLE);
        tvLoading.setText(getString(R.string.loading));
        ivState.setVisibility(View.GONE);
        adapter.setEnableLoadMore(true);
        adapter.getData().clear();
        adapter.notifyDataSetChanged();
        onLoadData();
    }

    private void onLoadData() {
        adapter.setEmptyView(mLoadingView);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                List<AddressBean> list = new ArrayList<>();
                AddressBean b0 = new AddressBean(1000,"JoinBean","15168353215","广东省",100,"广州市",101,"天河区",101001,"棠下街道泰安南路128号","家",false);
                list.add(b0);
                AddressBean b1 = new AddressBean(1001,"Three","15168358547","广东省",100,"广州市",101,"天河区",101001,"棠下街道大善社区南边西边大街2号","住宅",false);
                list.add(b1);
                AddressBean b2 = new AddressBean(1002,"吴茵其","14752638462","广西壮族自治区",100,"梧州市",101,"岑溪市",101001,"学府一号6栋2202","家",true);
                list.add(b2);
                AddressBean b3 = new AddressBean(1003,"李家在","18849632485","广东省",100,"湛江市",101,"雷州市",101001,"黄家村203号","公司",false);
                list.add(b3);
                AddressBean b4 = new AddressBean(1004,"曾毅呛","15841756328","广东省",100,"佛山市",101,"南海区",101001,"南海客运站1556号","公司",false);
                list.add(b4);
                AddressBean b5 = new AddressBean(1005,"郑灿许","18574957621","广东省",100,"广州市",101,"海珠区",101001,"赤岗街道夏塘路247号","家",false);
                list.add(b5);
                AddressBean b6 = new AddressBean(1006,"罗雄都","15575756241","广东省",100,"广州市",101,"天河区",101001,"棠下街道泰安南路171号","家",false);
                list.add(b6);
                AddressBean b7 = new AddressBean(1007,"黄群星","13424168574","广东省",100,"广州市",101,"荔湾区",101001,"芳村酒吧街1455号","公司",false);
                list.add(b7);
                AddressBean b8 = new AddressBean(1008,"李彦龙","15168384188","广东省",100,"广州市",101,"黄埔区",101001,"南云五路光谷科技园1978","公司",false);
                list.add(b8);
                AddressBean b9 = new AddressBean(1009,"周炼维","18647165321","广东省",100,"广州市",101,"越秀区",101001,"中山八1689号","家",false);
                list.add(b9);

//                for (int i = 0; i < 10; i++) {
//                    AddressBean bean = new AddressBean();
//                    bean.setId(i);
//                    bean.setName("吴茵其" + i);
//                    bean.setAddress("棠下街道泰安南路133号泰安花园");
//                    bean.setArea("天河区");
//                    bean.setCity("广州市");
//                    bean.setProvince("广东省");
//                    bean.setLabel("家");
//                    bean.setPhone("15168353219");
//                    bean.setDefault(i == 0 ? true:false);
//                    list.add(bean);
//                }
                setListAdapter(list);
            }
        },1000);

    }

    private void setListAdapter(List<AddressBean> mList) {
        try {
            onResetRefresh();
            if (mList.isEmpty()) {
                adapter.loadMoreEnd();
                adapter.setEnableLoadMore(false);
                setState(getString(R.string.no_data),R.mipmap.ic_empty);
            } else {
                adapter.setNewData(mList);
                adapter.loadMoreComplete();
                adapter.setEnableLoadMore(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setState(String string, int resId) {
        try {
            progressBar.setVisibility(View.GONE);
            tvLoading.setText(string);
            ivState.setImageResource(resId);
            ivState.setVisibility(View.VISIBLE);
            adapter.setEmptyView(mLoadingView);
            adapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void onResetRefresh() {
        try {
            if (refreshLayout.isRefreshing()) {
                refreshLayout.setRefreshing(false);
            }
            progressBar.setVisibility(View.GONE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onResetRefresh();
    }
}